package com.cxs.distributeddemo.slice;

import com.cxs.distributeddemo.ResourceTable;
import com.cxs.distributeddemo.util.KvStoreUtils;
import com.cxs.distributeddemo.util.UIUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.data.distributed.common.KvStoreException;
import ohos.data.distributed.user.SingleKvStore;

public class MainAbilitySlice extends AbilitySlice {
    Button btnWrite, btnDelete, btnRead, btnCloseDB, btnDelDB,btnReg;
    // 单版本分布式数据库
    SingleKvStore kvStore = null;
    private int moneyInt = 0,flag=0,temp_money=0;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 获取数据库
        //kvStore = KvStoreUtils.getKvStore(MainAbilitySlice.this);

        btnReg = (Button)findComponentById(ResourceTable.Id_btn_reg);
        btnReg.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if(flag == 0){
                    kvStore = KvStoreUtils.getKvStore(MainAbilitySlice.this);
                    kvStore.putInt("money", 0);
                    UIUtils.showTip(MainAbilitySlice.this, "账户注册成功");
                }
                else {
                    kvStore = KvStoreUtils.getKvStore(MainAbilitySlice.this);
                    kvStore.putInt("money", temp_money);
                    UIUtils.showTip(MainAbilitySlice.this, "账户登录成功");
                }
            }
        });

        btnWrite = (Button)findComponentById(ResourceTable.Id_btn_write);
        btnWrite.setClickedListener(c -> {
            // 向分布式数据库写入数据
            try {
                moneyInt = kvStore.getInt("money");
                moneyInt = moneyInt + 20;
                kvStore.putInt("money", moneyInt);
                UIUtils.showTip(MainAbilitySlice.this, "金额存入成功");
            } catch (Exception e){
                UIUtils.showTip(MainAbilitySlice.this, "请登录");
            }

        });

        btnRead = (Button)findComponentById(ResourceTable.Id_btn_read);
        btnRead.setClickedListener(c -> {
            // 从分布式数据库读取指定key的数据
            String moneyStr = "";
            try {
                kvStore = KvStoreUtils.getKvStore(MainAbilitySlice.this);
                moneyInt = kvStore.getInt("money");
                moneyStr = moneyInt + "";
                UIUtils.showTip(MainAbilitySlice.this, "余额："+moneyStr);
            } catch (KvStoreException exception) {
                UIUtils.showTip(MainAbilitySlice.this, "请登录");
            }

        });

        btnDelete = (Button)findComponentById(ResourceTable.Id_btn_del);
        btnDelete.setClickedListener(c -> {
            try {
                moneyInt = kvStore.getInt("money");
                moneyInt = moneyInt - 20;
                kvStore.putInt("money", moneyInt);
                //kvStore.delete("money");
                UIUtils.showTip(MainAbilitySlice.this, "金额取出成功");
            } catch (Exception e) {
                UIUtils.showTip(MainAbilitySlice.this, "请登录");
            }
        });

        btnCloseDB = (Button)findComponentById(ResourceTable.Id_btn_closedb);
        btnCloseDB.setClickedListener(c -> {
            kvStore = KvStoreUtils.getKvStore(MainAbilitySlice.this);
            temp_money = kvStore.getInt("money");
            KvStoreUtils.closeDB(kvStore);
            UIUtils.showTip(MainAbilitySlice.this, "退出成功");
            flag = 1;
        });

        btnDelDB = (Button)findComponentById(ResourceTable.Id_btn_deldb);
        btnDelDB.setClickedListener(c -> {
            KvStoreUtils.delDB();
            UIUtils.showTip(MainAbilitySlice.this, "注销账户成功");
            flag = 0;
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
